const router = require('koa-router')()
const { loginRedirect,loginCheck } = require('../middlewares/loginChecks')

router.get('/',loginRedirect,async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!',
    msg:"你好",
    isMe:true,
    blogList:[
      {
        id:1,
        title:'aaa'
      },
      {
        id:2,
        title:'bbb'
      },
      {
        id:3,
        title:'ccc'
      },
      {
        id:4,
        title:'ddd'
      },
      {
        id:5,
        title:'eee'
      },
    ]
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string1111'
})

router.get('/json', loginCheck,async (ctx, next) => {
  // const session = ctx.session
  // if(session.viewNum == null){
  //   session.viewNum = 0;
  // }
  // session.viewNum++
  ctx.body = {
    title: 'koa2 json',
    // viewNum: session.viewNum
  }
})

//动态路由
router.get('/profile/:userName',async (ctx,next)=>{
  const {userName}=ctx.params;
  ctx.body={
    title:'koa2 json profile page',
    userName
  }
})

router.get('/loadMore/:userName/:pageIndex',async (ctx,next)=>{
  const {userName,pageIndex}=ctx.params;
  ctx.body={
    title:'koa2 json profile page',
    userName,
    pageIndex
  }
})

module.exports = router
